package com.um.jdy.app.shop.coupons.impl;

import cn.hutool.core.util.StrUtil;
import com.um.jdy.app.shop.coupons.BaseCoupons;
import com.um.jdy.app.shop.coupons.Coupons;
import com.um.jdy.app.shop.entity.bo.CouponsBO;
import com.um.jdy.app.shop.entity.bo.GoodsBO;
import com.um.jdy.common.activity.entity.po.CouponsGoods;
import com.um.jdy.common.activity.service.CouponsGoodsService;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.shop.enums.CouponsEnum;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title 代金券
 * @Description
 * @Author renjunwei
 * @Date 2021/6/29 1:52 下午
 */
@Service
@Transactional
public class VoucherCouponsImpl extends BaseCoupons implements Coupons {

    @Autowired
    CouponsGoodsService couponsGoodsService;

    /**
     * 优惠券对象
     *
     * @param couponsBO
     */
    @Override
    public void setCouponsEntity(CouponsBO couponsBO) {
        this.couponsBO = couponsBO;
    }

    /**
     * 获取优惠券对象
     *
     * @return
     */
    @Override
    public CouponsBO getCouponsEntity() {
        return this.couponsBO;
    }

    /**
     * 是否满足优惠条件
     *
     * @return
     */
    @Override
    public boolean isMeet(List<GoodsBO> goodsBOList) {
        boolean isMeet = false;

        List<GoodsBO> couponsGoodsList = new ArrayList();//满足优惠券的商品

        //指定商品可用
        if(StringUtil.equals(this.couponsBO.getAvailable(), CouponsEnum.AvailableType.Part.name())){

            List<CouponsGoods> availableGoodsList = couponsGoodsService.getList(new String[]{"activity_coupons_id"},new String[]{this.couponsBO.getActivityCouponsId()});
            for(GoodsBO goodsBO : goodsBOList) {
                boolean goodsExist = availableGoodsList.stream()
                        .filter(item -> item.getShopGoodsId().equals(goodsBO.getShopGoodsId()) && (this.couponsBO.getIsActivityMerge().equals(YesOrNo.Yes.name()) || StrUtil.isEmpty(goodsBO.getMarketActivityId())))
                        .collect(Collectors.toList()).size() > 0;
                if(goodsExist){
                    couponsGoodsList.add(goodsBO);
                }
            }
        }else if(StringUtil.equals(this.couponsBO.getAvailable(), CouponsEnum.AvailableType.All.name())){

            //全部商品通用
            couponsGoodsList = goodsBOList.stream()
                    .filter(goodsEntity -> this.couponsBO.getIsActivityMerge().equals(YesOrNo.Yes.name()) || StrUtil.isEmpty(goodsEntity.getMarketActivityId()))
                    .collect(Collectors.toList());
        }

        this.couponsGoodsList = couponsGoodsList;
        this.couponsBO.setJoinCouponsPrice(couponsGoodsList.stream().mapToDouble(item -> item.getTotalPrice()).sum());

        //达到门槛
        if(this.couponsBO.getJoinCouponsPrice() >= this.couponsBO.getLimitMoney()){
            isMeet = true;
        }

        return isMeet;
    }

    /**
     * 返回参与优惠的商品列表
     *
     * @return
     */
    @Override
    public List<GoodsBO> getCouponsGoods() {
        return this.couponsGoodsList;
    }

    /**
     * 返回赠品列表（赠品券专用）
     *
     * @return
     */
    @Override
    public List<GoodsBO> getGiftGoods() {
        return null;
    }

    /**
     * 返回优惠收费说明项
     *
     * @return
     */
    @Override
    public Integer getFee() {
        return this.couponsBO.getMoney();
    }
}
